package com.jfinal.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.model.User;



public class UserController extends Controller {
   public void index(){
	   List<User> users = User.dao.find("select * from user");
	   setAttr("users",users);
	   System.out.println("得到数据"+users.size()+"个");
	   render("list.jsp");
   }
   
   public void form(){
	   Integer id = getParaToInt(0);
	   if(id!=null&&id>=0){
		   System.out.println(id+"tess"+User.dao.findById(id).getStr("name"));
		 setAttr("user",User.dao.findById(id));
	   }
	   render("form.jsp");
   }
   
   public void update(){
	   User user =  getModel(User.class,"user");
	   System.out.print("up"+user.getStr("name"));
	   user.update();
	   redirect("list");
   }
   
   public void submit(){
	 User user =  getModel(User.class,"user");
	 System.out.print("up"+user.getStr("name"));
	 user.save();
	
//	   String json = "{\"id\":\"123456\",\"system\":{\"sign\":\"0298159AACCADCBFA5FDEB520694D349\",\"key\":\"94a465066c9a4c7fafd729b52193f450\",\"time\":\"1468975672\",\"ver\":\"1.0\"},\"method\":\"token\\/accessToken\\/get\",\"params\":{\"phone\":\"15882781032\",\"userId\":\"654321\"}}";
	   String json = "{\"id\":\"123456\",\"system\":{\"sign\":\"56b76836bd412474b7a214908e81c10c\",\"time\":\"1468977867\",\"key\":\"94a465066c9a4c7fafd729b52193f450\",\"ver\":\"1.0\"},\"method\":\"token\\/accessToken\\/get\",\"params\":{\"phone\":\"15882781032\",\"userId\":\"654321\"}}";
	   System.out.println("json:"+json);
       PrintWriter out = null;
       BufferedReader in = null;
       String result = "";
       try {
           URL realUrl = new URL("https://open.ys7.com:443/api/method");
           // 打开和URL之间的连接
           URLConnection conn = realUrl.openConnection();
           // 设置通用的请求属性
           conn.setRequestProperty("accept", "*/*");
           conn.setRequestProperty("connection", "Keep-Alive");
           conn.setRequestProperty("user-agent",
                   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
           // 发送POST请求必须设置如下两行
           conn.setDoOutput(true);
           conn.setDoInput(true);
           // 获取URLConnection对象对应的输出流
           out = new PrintWriter(conn.getOutputStream());
           // 发送请求参数
           out.print(json);
           
           // flush输出流的缓冲
           out.flush();
           // 定义BufferedReader输入流来读取URL的响应
           in = new BufferedReader(
                   new InputStreamReader(conn.getInputStream()));
           String line;
           while ((line = in.readLine()) != null) {
               result += line;
           }
       } catch (Exception e) {
           System.out.println("发送 POST s请求出现异常！"+e);
           e.printStackTrace();
       }
       //使用finally块来关闭输出流、输入流
       finally{
           try{
               if(out!=null){
                   out.close();
               }
               if(in!=null){
                   in.close();
               }
           }
           catch(IOException ex){
               ex.printStackTrace();
           }
       }
      System.out.println("result"+result);
      System.out.println("result:list");
      redirect("list");
	 renderJson("{\"state\":\"success\",\"msg\":\"评论成功！\"}");
   }

   
   public void edit(){
	   form();
	   
   }
   
   public void del(){
	   User.dao.deleteById(getPara(0));
	   index();
   }
}
